package org.serverct.finalfight.game;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.serverct.finalfight.FinalFight;
import org.serverct.finalfight.game.uhc.solo.UHCSoloGame;
import org.serverct.finalfight.game.uhc.team.UHCTeamGame;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.basic.GameState;
import org.serverct.finalfight.internal.game.basic.GameType;
import org.serverct.finalfight.internal.info.SimpleInfoHandler;
import org.serverct.finalfight.util.CustomSettings;
import org.serverct.finalfight.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

public class GameManager {
    public static HashMap<String, BasicGame> arenas = new HashMap<>();

    public static void initialize() {
        File directory = new File(FinalFight.plugin.getDataFolder() + "/arenas/");
        if (!directory.exists()) {
            FinalFight.plugin.saveResource("arenas/example.yml", false);
        }
        for (File file : directory.listFiles()) {
            loadArena(file.getName().replace(".yml", ""));
        }
    }

    public static YamlConfiguration getArenaSettings(String id) {
        File file = new File(FinalFight.plugin.getDataFolder() + "/arenas/", id + ".yml");
        return YamlConfiguration.loadConfiguration(file);
    }

    public static void addArena(String id,GameType type) {
        File from = CustomSettings.getFile("arenas/", "tmp_"+type.name()+".yml");
        YamlConfiguration yaml = ResourceUtils.autoUpdateConfigs("arenas/", "tmp_"+type.name()+".yml");
        yaml.set("id", id);
        try {
            yaml.save(from);
        } catch (IOException e) {
            FinalFight.plugin.getLogger().warning("§c新建竞技场时出现错误！");
        }
        File to = new File(FinalFight.plugin.getDataFolder() + "/arenas/", id + ".yml");
        boolean returnCode = from.renameTo(to);
        if (returnCode) {
            loadArena(id);
        }
    }

    public static void loadArena(String id) {
        BasicGame arena = loadArena(getArenaSettings(id));
        if (arena != null) {
            arenas.put(id, arena);
        }
    }

    public static BasicGame loadArena(YamlConfiguration arenaSettings) {
        GameType type = GameType.valueOf(arenaSettings.getString("type"));
        switch (type) {
            case UHC_SOLO:
                return new UHCSoloGame(arenaSettings);
            case UHC_TEAM:
                return new UHCTeamGame(arenaSettings);
            case PUBG_SOLO:
            case PUBG_TEAM:

        }
        return null;
    }

    public static void unloadArena(String id) {
        forceStop(id);
        arenas.remove(id);
    }

    public static void reloadArena(String id) {
        unloadArena(id);
        loadArena(id);
    }

    public static void forceStop(String id) {
        BasicGame game = arenas.get(id);
        if (game == null) {
            return;
        }
        SimpleInfoHandler.broadcastMessage(game, true, false, "forcestop");
        for (UUID uuid : ((HashMap<UUID, BasicGame>) game.players.clone()).keySet()) {
            game.quit(Bukkit.getPlayer(uuid), false);
        }
        game.clear();
    }

    public static BasicGame getGame(Player player) {
        for (String id : arenas.keySet()) {
            BasicGame game = arenas.get(id);
            if (game.players.containsKey(player.getUniqueId())) {
                return game;
            }
        }
        return null;
    }

    public static void fastJoin(Player player){
        if(getGame(player)!=null){
            SimpleInfoHandler.sendMessage(player,true,"alreadyin");
            return;
        }
        String result="";
        int max=-1;
        for (String id : arenas.keySet()) {
            BasicGame game = arenas.get(id);
            if(game.state== GameState.WAITING){
                if(game.players.size()>max){
                    result=id;
                    max=game.players.size();
                }
            }
        }
        if(max==-1){
            SimpleInfoHandler.sendMessage(player,true,"nowaitingarena");
            return;
        }
        BasicGame game = arenas.get(result);
        game.join(player,false);
    }
}
